<?php 
	include($_SERVER['DOCUMENT_ROOT']."/headerins.php");
?>

<?php

try
{


	//Getting records (subAction)
	if($_GET["action"] == "sub")
	{
		//Get records from database
		$result = mysql_query("
            SELECT 
                OrderLineId, OrderId, MaterialId, Qty
            FROM OrderLine where Orderid = ".$_GET['OrderId']);
		//Add all records to an array
		$rows = array();
		while($row = mysql_fetch_array($result))
		{
		    $rows[] = $row;
		}

		//Return result to jTable
		$jTableResult = array();
		$jTableResult['Result'] = "OK";
		$jTableResult['Records'] = $rows;
		print json_encode($jTableResult);
	}
    
    
	//Getting records (listAction)
	if($_GET["action"] == "list")
	{
		//Get records from database
		$where = "";
            if(isset($_GET['OrderId']))
                $where  = ' where o.Orderid = '.$_GET['OrderId'];

		$result = mysql_query(" SELECT o.OrderLineId, o.MaterialId, o.Qty, o.Price1, o.Ball, m.Name, m.Code 
									FROM orderline o
									inner join material m on m.id = o.materialid
				 ".$where);

		//Add all records to an array
		$rows = array();
		while($row = mysql_fetch_array($result))
		{
		    $rows[] = $row;
		}

		$jTableResult = array();
		$jTableResult['Result'] = "Ok";
		$jTableResult['Records'] = $rows;
		print json_encode($jTableResult);

		//echo mysql_error();		

	}

	
	
	
	//Creating a new record (createAction)
	else if($_GET["action"] == "create")
	{
		//get materialID
		$materialID = 0;
		$result = mysql_query("SELECT ID FROM material where code = ". $_GET["code"]);
		
		$rows = array();
		while($row = mysql_fetch_array($result))
		   $materialID = $row['ID'];
		

		;

		//Insert record into database
		$result = mysql_query("
        	INSERT INTO `orderline`
			(OrderID, MaterialID, Qty, Price1, Ball)
		VALUES(
                '" . $_GET["orderID"] . "', 
                '" . $materialID . "',
                '" . $_GET["qty"] . "',
				'" . $_GET["price1"] . "',
				'" . $_GET["ball"] . "'
            );");

		$result2 = "";
		if (!$result) {
            $result2 = 'OrderLine/create/sql: ' . mysql_error(). ' '.$sql;
        }else
            $result2 = 'OrderLine/create/sql: successfull.';

		$jTableResult = array();
		$jTableResult['Result'] = $result2;
		print json_encode($jTableResult);


	}


	
	
	
	//Updating a record (updateAction)
	else if($_GET["action"] == "update")
	{
		//get materialID
		$materialID = 0;
		$result = mysql_query("SELECT ID FROM material where code = ". $_GET["MaterialID"]);
		
		$rows = array();
		while($row = mysql_fetch_array($result))
		   $materialID = $row['ID'];


		//Update record in database
		$result = mysql_query("
			UPDATE orderline SET 
                MaterialId = '" . $materialID . "',
                Qty    = '" . $_GET["Qty"] . "',
				Price1    = '" . $_GET["Price1"] . "',
				Ball    = '" . $_GET["Ball"] . "'
            WHERE OrderLineId = " . $_GET["OrderLineID"] . ";");

        if (!$result) {
            die('OrderLine/update/sql: ' . mysql_error(). $sql);
        }else
            print('OrderLine/update/sql: successfull.');
	}
	
	
	
	
	
	//Deleting a record (deleteAction)
	else if($_GET["action"] == "delete")
	{
		//Delete from database
		$result = mysql_query("DELETE FROM orderline WHERE OrderLineId = " . $_GET["OrderLineID"] . ";");

        if (!$result) {
            die('OrderLine/delete/sql: ' . mysql_error(). $sql);
        }else
            print('OrderLine/delete/sql: successfull.');
	}

	//Close database connection
	mysql_close($con);

}
catch(Exception $ex)
{
	 echo $ex;
}
	
?>
